package cn.edu.hbvtc.qms.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.edu.hbvtc.qms.domain.Category;
import cn.edu.hbvtc.qms.domain.Item;
import cn.edu.hbvtc.qms.service.IEvaluationService;
import cn.edu.hbvtc.qms.web.struts.form.ItemActionForm;

public class ItemAction extends AbstractBaseDispatchAction {

	private IEvaluationService evaluationService = null;

	public IEvaluationService getEvaluationService() {
		return evaluationService;
	}

	public void setEvaluationService(IEvaluationService evaluationService) {
		this.evaluationService = evaluationService;
	}

	// 跳转到评价表项管理列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List<Item> itemList = evaluationService.findItems();
		request.setAttribute("itemList", itemList);

		return mapping.findForward("list");
	}

	// 跳转到评价表项添加页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List<Category> categoryList = evaluationService.findCategorys();
		request.setAttribute("categoryList", categoryList);

		return mapping.findForward("add");
	}

	// 完成添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ItemActionForm iaf = (ItemActionForm) form;

		if (iaf.getCategoryId() == 0) {
			request.setAttribute("categoryIdRequired", "请选择所属模块！");
			return mapping.findForward("toAdd");
		}
		if (iaf.getContent() == null || "".equals(iaf.getContent())) {
			request.setAttribute("contentRequired", "请输入指标内容！");
			return mapping.findForward("toAdd");
		}
		if (iaf.getProportion() == 0 || "".equals(iaf.getProportion())) {
			request.setAttribute("proportionRequired", "请输入权重！");
			return mapping.findForward("toAdd");
		}
		Item item = new Item();
		iaf.setContent(iaf.getContent().replace("\r\n", "<br>"));
		PropertyUtils.copyProperties(item, iaf);

		Category category = evaluationService.findCategoryById(iaf
				.getCategoryId());
		item.setCategory(category);

		evaluationService.saveItem(item);

		return mapping.findForward("save");
	}

	// 跳转到评价表项修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ItemActionForm iaf = (ItemActionForm) form;

		Item item = evaluationService.findItemById(iaf.getId());
		request.setAttribute("item", item);

		List<Category> categoryList = evaluationService.findCategorys();
		request.setAttribute("categoryList", categoryList);

		return mapping.findForward("update");
	}

	// 完成修改
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ItemActionForm iaf = (ItemActionForm) form;

		if (iaf.getCategoryId() == 0) {
			request.setAttribute("categoryIdRequired", "请选择所属模块！");
			return mapping.findForward("toUpdate");
		}
		if (iaf.getContent() == null || "".equals(iaf.getContent())) {
			request.setAttribute("contentRequired", "请输入指标内容！");
			return mapping.findForward("toUpdate");
		}
		if (iaf.getProportion() == 0 || "".equals(iaf.getProportion())) {
			request.setAttribute("proportionRequired", "请输入权重！");
			return mapping.findForward("toUpdate");
		}
		Item item = evaluationService.findItemById(iaf.getId());
		iaf.setContent(iaf.getContent().replace("\r\n", "<br>"));
		PropertyUtils.copyProperties(item, iaf);
		
		Category category = evaluationService.findCategoryById(iaf.getCategoryId());
		item.setCategory(category);
		
		evaluationService.updateItem(item);
		
		return mapping.findForward("updateSave");
	}

	// 完成评价表项删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ItemActionForm iaf = (ItemActionForm)form;
		evaluationService.deleteItemById(iaf.getId());
		return mapping.findForward("delete");
	}
}
